package com.holdtime.jtt809.taxi.module.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.holdtime.jtt809.taxi.module.entity.CarRegisterDO;
import com.holdtime.jtt809.taxi.module.entity.mapper.CarRegisterMapper;
import com.holdtime.jtt809.taxi.module.model.vo.CarRegisterPageReqVO;
import com.holdtime.jtt809.taxi.module.model.vo.CarRegisterSaveReqVO;
import com.holdtime.jtt809.taxi.module.service.CarRegisterService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.holdtime.jtt809.taxi.module.enums.Taxi809ErrorCodeConstants.CAR_REGISTER_NOT_EXISTS;

/**
 * 车辆注册信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class CarRegisterServiceImpl implements CarRegisterService {

    @Resource
    private CarRegisterMapper carRegisterMapper;

    @Override
    public String createCarRegister(CarRegisterSaveReqVO createReqVO) {
        // 插入
        CarRegisterDO carRegister = BeanUtils.toBean(createReqVO, CarRegisterDO.class);
        carRegisterMapper.insert(carRegister);
        // 返回
        return carRegister.getId();
    }

    @Override
    public void updateCarRegister(CarRegisterSaveReqVO updateReqVO) {
        // 校验存在
        validateCarRegisterExists(updateReqVO.getId());
        // 更新
        CarRegisterDO updateObj = BeanUtils.toBean(updateReqVO, CarRegisterDO.class);
        carRegisterMapper.updateById(updateObj);
    }

    @Override
    public void deleteCarRegister(String id) {
        // 校验存在
        validateCarRegisterExists(id);
        // 删除
        carRegisterMapper.deleteById(id);
    }

    private void validateCarRegisterExists(String id) {
        if (carRegisterMapper.selectById(id) == null) {
            throw exception(CAR_REGISTER_NOT_EXISTS);
        }
    }

    @Override
    public CarRegisterDO getCarRegister(String id) {
        return carRegisterMapper.selectById(id);
    }

    @Override
    public PageResult<CarRegisterDO> getCarRegisterPage(CarRegisterPageReqVO pageReqVO) {
        return carRegisterMapper.selectPage(pageReqVO);
    }

}